package com.hoapital.outpbillwork.billWorkHandler;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hoapital.outpbillwork.module.entity.*;
import com.hoapital.outpbillwork.module.service.*;
import com.hospital.api.RemoteBillService;
import com.hospital.common.core.constants.AllFinalValueUtils;
import com.hospital.common.core.exception.ExceptionHelp;
import com.hospital.common.core.module.controller.vo.CancelOrder;
import com.hoapital.outpbillwork.module.controller.vo.OrderInfo;
import com.hospital.common.core.module.controller.vo.Receive;
import com.hospital.common.core.module.controller.vo.ReceiveDate;
import com.hospital.common.core.module.entity.BaseRequest;
import com.hospital.common.core.result.ErrorTip;
import com.hospital.common.core.result.Tip;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lvyao
 * @description 策略类
 * @date 2021/2/25
 */

public class BillWorkHandlerStrategy {

    private BillWorkHandler billWorkHandler;

    public static BillWorkHandler getChargeType(String billType) {

        if (billType != null && !billType.equals("")) {
            if (billType.equals(AllFinalValueUtils.TEST)) {
                return new LabBillJob();
            } else if (billType.equals(AllFinalValueUtils.EXAM)) {
                return new ExamBillJob();
            } else if (billType.equals(AllFinalValueUtils.WEST_DRUG)) {
                return new PrescBillJob();
            } else if (billType.equals(AllFinalValueUtils.EAST_DRUG)) {
                return new PrescBillJob();
            }
        } else {
            return new AllBillJob();
        }
        return null;
    }

    public static BillWorkHandler getSendType(String billType) {
        if (billType.equals(AllFinalValueUtils.EXAM)) {
            return new ExamOrderBusiness();
        } else if (billType.equals(AllFinalValueUtils.TEST)) {
            return new LabOrderBusiness();
        } else if (billType.equals(AllFinalValueUtils.WEST_DRUG)) {
            return new PrescOrderBusiness();
        } else if (billType.equals(AllFinalValueUtils.EAST_DRUG)) {
            return new PrescOrderBusiness();
        } else {
            return new OtherTypeOrderBusiness();
        }
    }


    public BillWorkHandlerStrategy(BillWorkHandler billWorkHandler) {
        this.billWorkHandler = billWorkHandler;
    }

    public Tip getResult(Receive receive, HttpServletRequest request) throws ExceptionHelp {
        return this.billWorkHandler.billWork(receive, request);
    }

    public Tip getResult(String requestVal, HttpServletRequest request, OutpOrderDescTempService outpOrderDescTempService) throws ExceptionHelp {
        BaseRequest baseRequest = JSON.parseObject(requestVal, BaseRequest.class);
        if (baseRequest.getHospitalCode() == null || baseRequest.getHospitalName() == null ||
                baseRequest.getHospitalCode().trim().equals("") || baseRequest.getHospitalName().trim().equals("")) {
            return new ErrorTip("1001", "入参医疗机构标识、名称出错！");
        }
        Receive receive = null;
        try {
            receive = JSON.parseObject(requestVal, Receive.class);
            Tip tip = outpOrderDescTempService.orderCheckStock(receive.getSettlePatientInfo().get(0).getBillNo());
            if (tip.getCode() != (AllFinalValueUtils.SUCCESS_CODE_INT)) {
                return tip;
            }
        } catch (Exception e) {
            return new ErrorTip("1001", "入参格式转换出错:" + e.getMessage());
        }
//        if (receive.getOutpOrInp().equals("01")) {
        return this.billWorkHandler.billWork(receive, request);
//        }else {
//            return this.billWorkHandler.billInpWork(receive, request);
//        }
    }

    public Tip getResult(OrderInfo orderInfo, HttpServletRequest request, RemoteBillService remoteBillService) throws ExceptionHelp {
        return this.billWorkHandler.orderWork(orderInfo, request, remoteBillService);
    }

    public Tip getResult(CancelOrder cancelOrder, HttpServletRequest request) throws ExceptionHelp {
        return this.billWorkHandler.orderCancel(cancelOrder, request);
    }

    public Tip checkOutpOrder(Receive requestVal, OutpBillItemsTempService outpBillItemsTempService, OutpOrderDescTempService outpOrderDescTempService) {
        Tip tip = checkOutpCommOrder(requestVal);
        if (tip != null) {
            return tip;
        }
        List<OutpBillItemsTemp> outpBillItemsTempList = outpBillItemsTempService.getOutpBillItemsTemp(requestVal.getPrimaryDate(), requestVal.getPrimaryNo());
        OutpOrderDescTemp orderDescTempList = outpOrderDescTempService.getOrderTempDate(requestVal.getPrimaryDate(), requestVal.getPrimaryNo());
        if ((outpBillItemsTempList != null && !outpBillItemsTempList.isEmpty() && orderDescTempList != null) || requestVal.getTaskId().equals("")) {
            return null;
        }
        return new ErrorTip("1002", "请求数据中就诊时间：" + requestVal.getPrimaryDate() + " 就诊号：" + requestVal.getPrimaryNo() + "暂存表中数据查询异常请联系相关人员处理");
    }

    public Tip checkOutpOrder(Receive requestVal, OutpBillItemsTempService outpBillItemsTempService, OutpOrderDescTempService outpOrderDescTempService, DrugPrescMasterService drugPrescMasterService, OutpDoctorPrescMasterService outpDoctorPrescMasterService) {
        Tip tip = checkOutpCommOrder(requestVal);
        if (tip != null) {
            return tip;
        }
        List<OutpBillItemsTemp> outpBillItemsTempList = outpBillItemsTempService.getOutpBillItemsTemp(requestVal.getPrimaryDate(), requestVal.getPrimaryNo());
        OutpOrderDescTemp orderDescTempList = outpOrderDescTempService.getOrderTempDate(requestVal.getPrimaryDate(), requestVal.getPrimaryNo());
        DrugPrescMaster drugPrescmaster = drugPrescMasterService.getDrugPrescmaster(requestVal.getPrimaryDate(), requestVal.getPrimaryNo());
        OutpDoctorPrescMaster outpDocPresc = outpDoctorPrescMasterService.getOutpDocPresc(requestVal.getPrimaryDate(), requestVal.getPrimaryNo());
        if ((outpBillItemsTempList != null && !outpBillItemsTempList.isEmpty() && orderDescTempList != null && (drugPrescmaster != null || outpDocPresc != null)) || requestVal.getTaskId().equals("")) {
            return null;
        }
        return new ErrorTip("1002", "请求数据中就诊时间：" + requestVal.getPrimaryDate() + " 就诊号：" + requestVal.getPrimaryNo() + "数据查询异常请联系相关人员处理");
    }

    public Tip checkOutpOrder(Receive requestVal, LabTestItemsService labTestItemsService, OutpBillItemsTempService outpBillItemsTempService, OutpOrderDescTempService outpOrderDescTempService) {
        Tip tip = checkOutpCommOrder(requestVal);
        if (tip != null) {
            return tip;
        }
        List<ReceiveDate> receiveDateList = requestVal.getData();
        for (ReceiveDate receiveDate : receiveDateList) {
            QueryWrapper<LabTestItems> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("test_no", requestVal.getApplyNo()).eq("charge_flag", "1").eq("item_code", receiveDate.getItemCode());
            List<LabTestItems> labTestItemsList = labTestItemsService.list(queryWrapper);
            if (labTestItemsList != null && labTestItemsList.size() > 0) {
                return new ErrorTip("1002", "请求计费的检验项目中包含已计费项目请核对！");
            }
        }
        List<OutpBillItemsTemp> outpBillItemsTempList = outpBillItemsTempService.getOutpBillItemsTemp(requestVal.getPrimaryDate(), requestVal.getPrimaryNo());
        OutpOrderDescTemp orderDescTempList = outpOrderDescTempService.getOrderTempDate(requestVal.getPrimaryDate(), requestVal.getPrimaryNo());
        if (outpBillItemsTempList != null && !outpBillItemsTempList.isEmpty() && orderDescTempList != null) {
            return null;
        }
        return new ErrorTip("1002", "请求数据中就诊时间：" + requestVal.getPrimaryDate() + " 就诊号：" + requestVal.getPrimaryNo() + "暂存表中数据查询异常请联系相关人员处理");
    }

    public Tip checkOutpCommOrder(Receive requestVal) {
        if (requestVal.getTaskId() != null && requestVal.getPrimaryDate() != null && requestVal.getPrimaryNo() != null) {
            return null;
        }
        return new ErrorTip("1002", "计费失败,传入参数必填项存在空值，请检查入参!");
    }

    public Tip checkInpOrder(Receive requestVal, InpBillDetailService inpBillDetailService) {
        int patientInHospital = inpBillDetailService.getPatientInHospital(requestVal.getApplyNo());
        if (patientInHospital > 0) {
            return null;
        }
        return new ErrorTip("1002", "计费失败,病人此次就诊记录不在院!");
    }

}
